// SUMMARY STATISTICS IN APPENDIX


/////////////////////////////////////////////
// Table B1


// upper panel
scalar drop _all
eststo clear
use "data/figure1/simulation_main.dta", clear


global var ds d m inter_d_m dm y_full
bysort dm: eststo: quietly estpost summarize $var, listwise


esttab using "results/sim_sum.tex", cells("mean(fmt(3)) sd(fmt(3))") label nodepvar replace
esttab , cells("mean(fmt(3)) sd(fmt(3))") label nodepvar 


// middle panel
eststo clear
estpost corr $var, matrix listwise
esttab using "results/sim_corr.tex", unstack not noobs compress nostar  b(2) replace
esttab, unstack not noobs compress nostar  b(2)


// lower panel
use "data/figure1/simulation_main_intermediate.dta", clear

foreach s in 10 20 30 40 50 60 70 80 90 {
corr d m if sample`s'==1
scalar ro=r(rho)
corr d m if sample`s'==1, cov
matrix a`s'=[ro \ r(cov_12)]
}
matrix a =[a10, a20, a30, a40, a50, a60, a70, a80, a90]
esttab matrix(a, fmt(2)) using "results/sim_corrcov.tex" ,  replace
esttab matrix(a, fmt(2))



	
///////////////////////////////////////////////////////////
// Table B2
eststo clear

quietly {
******************************
**  PRELIMINARY OPERATIONS
******************************

******************************
** Lagged values
******************************
use "data/table1/enricoall2.dta", clear
sort state district year
by state district year: g n = _n
keep if n<=2

replace year = year +2
g lagged = realada

	// Matteo adds controls
	recode sex (1=1) (2=0), g(male)
	tab lastoccup, g(doccup)
	tab collegeattend, g(dcoll)
	tab milit, g(dmil)
	tab second, g(dedu)
	global contr male relatives_shrt age ///
	doccup1 doccup2 doccup3 doccup4 doccup5 doccup6 doccup7 ///
	dcoll1 dcoll2 dcoll3 dcoll4 ///
	dmil1 dmil2 dmil3 ///
	dedu1 dedu2 dedu3
	foreach var in $contr {
	g lag_`var'=`var'
	}
keep state district year n lagged lag_*
sort state district year n
save tmp, replace

******************************
** Simultaneous values
******************************
use "data/table1/enricoall2.dta", clear
sort state district year
by state district year: g n = _n
keep if n<=2

drop lagada*
sort state district year n
merge state district year n using tmp
rm tmp.dta

* drop if last time was a redistricting year
drop if year ==1952 | year==1962 | year==1972 | year==1982 | year==1992
*keep if lagdemvoteshare>.48 & lagdemvoteshare<.52
drop democrat
g       democrat = 1 if demvoteshare>=.5
replace democrat = 0 if demvoteshare<.5

g       lagdemocrat = 1 if lagdemvoteshare >=.5
replace lagdemocrat = 0 if lagdemvoteshare <.5
g    score = realada
g lagscore = lagged
egen id = group(state district year n)
keep if score ~=. & lagscore ~=. | year==1946
keep if democrat~=. & lagdemocrat ~=. | year==1946
keep if id ~=.
replace lagdemocrat=. if year==1946

********************************
** Matteo adds Majority Status
********************************
g dem_maj = 1
replace dem_maj = 0 if congress==80 | congress==83
g majority = .
replace majority = democrat if dem_maj==1
replace majority = 1 - democrat if dem_maj==0
g lagdem_maj = 1
replace lagdem_maj = 0 if congress==81 | congress==84
replace lagdem_maj = . if congress==80
g lagmajority = .
replace lagmajority = lagdemocrat if lagdem_maj==1
replace lagmajority = 1 - lagdemocrat if lagdem_maj==0

********************************
** Matteo adds Time dummies
********************************
quietly: tab congress, g(dcon)

********************************
** Matteo adds Running variable
********************************
g dgap = demvoteshare-0.5
g lagdgap = lagdemvoteshare-0.5
g rdgap = dgap * democrat
g lagrdgap = lagdgap * lagdemocrat
g dgap2=dgap*dgap
g lagdgap2=lagdgap*lagdgap
g rdgap2=rdgap*rdgap
g lagrdgap2=lagrdgap*lagrdgap

********************************
** Matteo adds Contemporaneous Controls
********************************
recode sex (1=1) (2=0), g(male)
tab lastoccup, g(doccup)
tab collegeattend, g(dcoll)
tab milit, g(dmil)
tab second, g(dedu)

********************************
** Matteo rename vars shorter
********************************
g d = democrat
g lagd=lagdemocrat
g m = majority
g lagm = lagmajority
g dm = dem_maj
g lagdm = lagdem_maj
g inter_d_m = d*m


******************************
** END PRELIMINARY OPERATIONS
******************************
}


// Vars
global var dgap d m inter_d_m dm score 

bysort dm: eststo: quietly estpost summarize $var, listwise


esttab using "results/leeetal_sum.tex", cells("mean(fmt(3)) sd(fmt(3))") label nodepvar replace
esttab , cells("mean(fmt(3)) sd(fmt(3))") label nodepvar 




////////////////////////////////////////////////////////////////
// Table B4
eststo clear 
use "data/table2/table2.dta", clear

g inter_d_m = dem*maj

// Vars
global var dgap dem maj inter_d_m demmaj real_ada 

bysort demmaj: eststo: quietly estpost summarize $var, listwise



esttab using "results/ada_anderson_sum.tex", cells("mean(fmt(3)) sd(fmt(3))") label nodepvar replace

esttab , cells("mean(fmt(3)) sd(fmt(3))") label nodepvar 


///////////////////////////////////////////////////////////////////////////

// Table B5
eststo clear 
use "data/table3/fh_fia_inter.dta", clear

g inter_d_m=dem*maj

// Vars
global var rv dem maj inter_d_m demmaj dv_nongroup_inc 

bysort demmaj: eststo: quietly estpost summarize $var, listwise

esttab using "results/moneynongroup_sum.tex", cells("mean(fmt(3)) sd(fmt(3))") label nodepvar replace
esttab , cells("mean(fmt(3)) sd(fmt(3))") label nodepvar 
